package com.fzdy.dataplatform.mongodb;


import com.fzdy.dataplatform.core.vo.Page;
import com.fzdy.dataplatform.core.vo.Seach;
import com.fzdy.dataplatform.dao.vo.DeviceKtvListVoDao;
import com.fzdy.dataplatform.entity.DeviceKtvListVo;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.*;

@Repository
@Transactional
public class DeviceDataDaoImpl implements DeviceDataDao {

    @Autowired
    MongoDBManager manager;
    @Autowired
    DeviceKtvListVoDao deviceKtvListVoDao;

    @Override
    public void findBySn(String sn, Page<DeviceData> page) {
        String dbBase =page.getCreatedate().replace("-","");
        DeviceKtvListVo vo = new DeviceKtvListVo();
        vo.setSn(sn);
        List<DeviceData> li;
        Long total;
        List<DeviceKtvListVo> list;
        try {
            list = deviceKtvListVoDao.findByExampleEq(vo, Order.asc("seq"));
            manager.getMongoCon(dbBase, "devicedata");
            li = manager.mongdbQueryPageBysnAndCreatedate(sn, page.getCreatedate(), page);
            total = manager.mongCountBysnAndCreatedate(sn, page.getCreatedate());
            setlist(li, list);
            page.setList(li);
            page.setTotalRow(total);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    private void setlist(List<DeviceData> li, List<DeviceKtvListVo> list) {
        try {
            for (DeviceData data : li) {
                for (int i = 0; i < list.size(); i++) {
                    if (data.getPrivatedata() != null && i < data.getPrivatedata().size()) {
                        KTV ktv = data.getPrivatedata().get(i);
                        ktv.setKName(list.get(i).getName());
                        ktv.setTType(list.get(i).getTType());
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void findLogBySn(String sn, Page<DeviceData> page) {
        try {
            String dbBase =page.getCreatedate().replace("-","");
//        manager.getMongoCon("db", "devicelog");
            manager.getMongoCon(dbBase, "devicelog");
            List<DeviceData> li = manager.mongdbQueryPageBysnAndCreatedate(sn, page.getCreatedate(), page);
            Long total = manager.mongCountBysnAndCreatedate(sn, page.getCreatedate());
            page.setList(li);
            page.setTotalRow(total);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    @Override
    public Map<String, Object> getchartdata(Seach seach) {
        try {
            Date date = new Date();
            String dbBase = seach.getCreatedate().replace("-","");
            manager.getMongoCon(dbBase, "devicedata");
            List<DeviceData> li = manager.mongGetAllBysnAndCreatedate(seach.getSn(), seach.getCreatedate());
            List time = new ArrayList();
            List value = new ArrayList();
            for (DeviceData data : li) {
                if (data.getPrivatedata() != null&&data.getPrivatedata().size()>seach.getId()) {
                    time.add(data.getCreatedate());
                    value.add(data.getPrivatedata().get(seach.getId()).getV());
                }
            }
            Map<String, Object> map = new HashMap<>();
            map.put("createdate", time);
            map.put("value", value);
            return map;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
